<?php
/**
 *
 *  
 * Date: 2024/1/14
 * Email: <   >
 */

namespace app\controller;
use app\api\model\Cards as CardsDB;
use app\controller\model\WaAdmins as WaAdminsDB;
use app\api\model\ManualCards as ManualCardsDB;
use app\api\model\Wallets;
use app\api\model\PayCommon as PayCommonDB;

use app\traits\OpChickenCard;
use support\Request;
use Tinywan\Jwt\JwtToken;
use Illuminate\Support\Carbon;


class  UserController extends Base
{
    use OpChickenCard;
    public function index()
    {
        return view('admin.kt.index');
    }

    //登录
    public function getToken($res)
    {
        $data = [
            'id' => $res['id'], // 这里必须是一个全局抽象唯一id
            'username' => $res['username'],
            'password' => $res['password'],
        ];
        $token = JwtToken::generateToken($data);
        return $token;
    }
    public function login(Request $request)
    {
        $data = $request->only(['password', 'username']);
        $ha_login_password = WaAdminsDB::where('username', $data['username'])->value('password');
        $res = false;
        if (password_verify($data['password'], $ha_login_password)) {
            $res = WaAdminsDB::where('username', $data['password'])
                ->select('id', 'username', 'password')
                ->first();
        }
        if ($res) {
            $data = $this->getToken($res);
            return $this->json(2, '登录成功!',$data);
        } else {
            return $this->json(4, '账号或密码错误');
        }
    }

    //获取用户信息
    public  function userInfo(){
        $id = JwtToken::getCurrentId();
        $res = WaAdminsDB::where('id', $id)->first();
        return json([
            'code' => 2,
            'msg' => '成功',
            'data' => $res,
        ]);
    }

    //获取手动发卡列表
    public function manualCardsList(Request $request){
        $consumer = $request->get('consumer', '');
        $res = ManualCardsDB::where('consumer', 'like', '%' . $consumer . '%')->orderBy('id', 'desc')
            ->paginate(10);
        return json([
            'code' => 2,
            'msg' => 'ok',
            'data' => $res,
        ]);
    }
    public function manualCardsadd(Request $request, ManualCardsDB $DB, Wallets $Wallets)
    {
        $req = $request->only(['issuer', 'consumer', 'green_card_1', 'green_card_2', 'green_card_3', 'green_card_4',
            'green_card_5', 'blue_card_1', 'blue_card_2','gold_card_1','red_card_1','black_card_1']);
        $req['created_at'] = Carbon::now()->toDateTimeString();
        //获取用户信息
        $req['issuer'] = '管理员';
        //添加发卡记录
        $DB->insertGetId($req);
        $wallet_db  = $Wallets->where('wallet_user_id', $req['consumer'])->first();
        $new_count = [
            'green_card_1'=>$req['green_card_1']+$wallet_db['green_card_1'],
            'green_card_2'=>$req['green_card_2']+$wallet_db['green_card_2'],
            'green_card_3'=>$req['green_card_3']+$wallet_db['green_card_3'],
            'green_card_4'=>$req['green_card_4']+$wallet_db['green_card_4'],
            'green_card_5'=>$req['green_card_5']+$wallet_db['green_card_5'],
            'blue_card_1'=>$req['blue_card_1']+$wallet_db['blue_card_1'],
            'blue_card_2'=>$req['blue_card_2']+$wallet_db['blue_card_2'],
            'gold_card_1'=>$req['gold_card_1']+$wallet_db['gold_card_1'],
            'red_card_1'=>$req['red_card_1']+$wallet_db['red_card_1'],
            'black_card_1'=>$req['black_card_1']+$wallet_db['black_card_1'],
        ];
        //更新钱包数据
        $res = $Wallets->where('wallet_user_id', $req['consumer'])->update($new_count);
        //母鸡卡使用记录
        //卡片数量
        $card1= $request->only(['green_card_1', 'green_card_2', 'green_card_3', 'green_card_4',
            'green_card_5', 'blue_card_1', 'blue_card_2','gold_card_1','red_card_1','black_card_1']);
        $sum = $req['green_card_1']+$req['green_card_2']+$req['green_card_3']+$req['green_card_4']+$req['green_card_5']+
            $req['blue_card_1']+$req['blue_card_2']+$req['gold_card_1']+$req['red_card_1']+$req['black_card_1'];
        //添加卡片记录
        CardsDB::insertGetId([
            'uid' => $req['consumer'],
            'card' => json_encode($card1),
            'count' => $sum,
            'type' => '后台空投',
            'yu_e' => $this->cardsYue($req['consumer']),
            'created_at' => Carbon::now()->toDateTimeString()
        ]);
        $code = 2;
        $msg = '添加成功!';
        return json([
            'code' => $code,
            'msg' => $msg,
            'data' => $res,
        ]);
    }

    /**
     * 千星支付配置
     *
     */
    public function payList(Request $request){
        $name = $request->get('name', '');
        $res = PayCommonDB::where('name', 'like', '%' . $name . '%')->orderBy('id', 'asc')
            ->paginate(10);
        return json([
            'code' => 2,
            'msg' => 'ok',
            'data' => $res,
        ]);
    }
    public function payAdd(Request $request){
        $req = $request->only(['name', 'code', 'price', 'is_use', 'is_fix']);
        $res = PayCommonDB::where('name',$req['name'])->first();
        if (!$res) {
            $req['created_at'] = Carbon::now()->toDateTimeString();
            $res = PayCommonDB::insertGetId($req);
            $code = 2;
            $msg = '添加成功!';
        } else {
            $code = 0;
            $msg = '已存在，请修改';
        }
        return json([
            'code' => $code,
            'msg' => $msg,
            'data' => $res,
        ]);
    }
    public function payDel(Request $request){
        $id = $request->post('id');
        $res = PayCommonDB::where('id',$id)->first();
        if (!$res) {
            $code = 0;
            $msg = '没有找到!';
        } else {
            $res = PayCommonDB::where('id',$id)->delete();
            $code = 2;
            $msg = '已删除!';
        }
        return json([
            'code' => $code,
            'msg' => $msg,
            'data' => $res,
        ]);
    }
    public function payUpdate(Request $request){
        $req = $request->only(['id','name', 'code', 'price', 'is_use', 'is_fix']);
        $res = PayCommonDB::where('id',$req['id'])->first();
        if (!$res) {
            $code = 0;
            $msg = '未找到!';
        } else {
            $req['updated_at'] = Carbon::now()->toDateTimeString();
            $res = PayCommonDB::where('id',$req['id'])->update($req);
            $code = 0;
            $msg = '已修改';
        }
        return json([
            'code' => $code,
            'msg' => $msg,
            'data' => $res,
        ]);
    }
}